import {
    Controller,
    Get,
    HttpStatus,
    Query,
    DefaultValuePipe,
    Post,
    Put,
    Body,
    Param,
} from '@nestjs/common';
import {BookingService} from './booking.service';
import {
    ApiTags,
    ApiOperation,
    ApiResponse,
    ApiQuery,
    ApiBearerAuth,
} from '@nestjs/swagger';
import {PublicVoSuccess, PublicVoError} from '../utils/publicVo/index';
import {generateParseIntPipe} from '../utils/paging/index';
import {
    RequireLogin,
    RequirePermission,
    UserInfo,
} from 'src/middleware/RBAC/custom.decorator';
import {CreateBookingDto} from './dto/create-booking.dto';
import {BookingListVo} from './vo/booking-list.vo';
import {ExamineBookingDto} from "./dto/examine-booking.dto";
import {DisabledUserDto} from "../user/dto/disabled-user.dto";

@ApiTags('预定管理模块')
@Controller('booking')
export class BookingController {
    constructor(private readonly bookingService: BookingService) {
    }

    // 插入初始化数据
    @Get('init-data')
    @ApiOperation({summary: '初始化数据', description: '插入初始化数据接口'})
    @ApiResponse({
        status: HttpStatus.OK,
        description: '请求成功',
        type: PublicVoSuccess,
    })
    @ApiResponse({
        status: HttpStatus.BAD_REQUEST,
        description: '请求失败',
        type: PublicVoError,
    })
    async initData() {
        await this.bookingService.initData();
        return 'done';
    }

    @Get()
    @ApiOperation({summary: '预定列表', description: '预定列表接口'})
    @ApiQuery({
        name: 'pageNum',
        type: String,
        description: '页码',
        required: true,
    })
    @ApiQuery({
        name: 'pageSize',
        type: String,
        description: '页数',
        required: true,
    })
    @ApiQuery({
        name: 'username',
        type: String,
        description: '预定人',
        required: false,
    })
    @ApiQuery({
        name: 'meetingName',
        type: String,
        description: '会议室名称',
        required: false,
    })
    @ApiQuery({
        name: 'meetingPosition',
        type: String,
        description: '会议室位置',
        required: false,
    })
    @ApiQuery({
        name: 'bookingTimeRangeStart',
        type: String,
        description: '预定开始时间',
        required: false,
    })
    @ApiQuery({
        name: 'bookingTimeRangeEnd',
        type: String,
        description: '预定结束时间',
        required: false,
    })
    @ApiBearerAuth()
    @RequireLogin()
    @RequirePermission()
    @ApiResponse({
        status: HttpStatus.OK,
        description: '请求成功',
        type: BookingListVo,
    })
    @ApiResponse({
        status: HttpStatus.BAD_REQUEST,
        description: '请求失败',
        type: PublicVoError,
    })
    async list(
        @Query('pageNum', new DefaultValuePipe(1), generateParseIntPipe('pageNo'))
            pageNum: number,
        @Query(
            'pageSize',
            new DefaultValuePipe(10),
            generateParseIntPipe('pageSize'),
        )
            pageSize: number,
        @Query('username') username: string,
        @Query('meetingName') meetingName: string,
        @Query('meetingPosition') meetingPosition: string,
        @Query('bookingTimeRangeStart') bookingTimeRangeStart: number,
        @Query('bookingTimeRangeEnd') bookingTimeRangeEnd: number,
    ) {
        return this.bookingService.findAll(
            pageNum,
            pageSize,
            username,
            meetingName,
            meetingPosition,
            bookingTimeRangeStart,
            bookingTimeRangeEnd,
        );
    }

    @Post()
    @ApiOperation({summary: '预定会议室', description: '预定会议室接口'})
    @ApiBearerAuth()
    @RequireLogin()
    @RequirePermission()
    @ApiResponse({
        status: HttpStatus.OK,
        description: '请求成功',
        type: PublicVoSuccess,
    })
    @ApiResponse({
        status: HttpStatus.BAD_REQUEST,
        description: '请求失败',
        type: PublicVoError,
    })
    async create(
        @Body() booking: CreateBookingDto,
        @UserInfo('userId') userId: number,
    ) {
        await this.bookingService.create(booking, userId);
        return 'success';
    }

    @Put('apply/:id')
    @ApiOperation({summary: '通过', description: '审核通过接口'})
    @ApiBearerAuth()
    @RequireLogin()
    @RequirePermission()
    @ApiResponse({
        status: HttpStatus.OK,
        description: '请求成功',
        type: PublicVoSuccess,
    })
    @ApiResponse({
        status: HttpStatus.BAD_REQUEST,
        description: '请求失败',
        type: PublicVoError,
    })
    async apply(@Param('id') id: number) {
        return this.bookingService.apply(id);
    }

    @Put('reject/:id')
    @ApiOperation({summary: '驳回', description: '审核驳回接口'})
    @ApiBearerAuth()
    @RequireLogin()
    @RequirePermission()
    @ApiResponse({
        status: HttpStatus.OK,
        description: '请求成功',
        type: PublicVoSuccess,
    })
    @ApiResponse({
        status: HttpStatus.BAD_REQUEST,
        description: '请求失败',
        type: PublicVoError,
    })
    async reject(@Param('id') id: number) {
        return this.bookingService.reject(id);
    }

    @Put('cancel/:id')
    @ApiOperation({summary: '取消预定', description: '取消预定接口'})
    @ApiBearerAuth()
    @RequireLogin()
    @RequirePermission()
    @ApiResponse({
        status: HttpStatus.OK,
        description: '请求成功',
        type: PublicVoSuccess,
    })
    @ApiResponse({
        status: HttpStatus.BAD_REQUEST,
        description: '请求失败',
        type: PublicVoError,
    })
    async unbind(@Param('id') id: number) {
        return this.bookingService.unbind(id);
    }

    @Put('examine')
    @ApiOperation({summary: '审批', description: '审批接口'})
    @ApiBearerAuth()
    @RequireLogin()
    @RequirePermission()
    @ApiResponse({
        status: HttpStatus.OK,
        description: '请求成功',
        type: PublicVoSuccess,
    })
    @ApiResponse({
        status: HttpStatus.BAD_REQUEST,
        description: '请求失败',
        type: PublicVoError,
    })
    async examine(@Body() examineBookingDto: ExamineBookingDto,) {
        await this.bookingService.examineBooking(examineBookingDto);
        return 'success';
    }

    @Post('urge/:id')
    @ApiOperation({summary: '催办', description: '催办接口'})
    @ApiBearerAuth()
    @RequireLogin()
    @RequirePermission()
    @ApiResponse({
        status: HttpStatus.OK,
        description: '请求成功',
        type: PublicVoSuccess,
    })
    @ApiResponse({
        status: HttpStatus.BAD_REQUEST,
        description: '请求失败',
        type: PublicVoError,
    })
    async urge(@Param('id') id: number) {
        return this.bookingService.urge(id);
    }
}
